import {
  CreateUser,
  login,
  UpdateUser,
  GetMemberInfo
} from "@/network/user";
import {
  getToken,
  setToken,
  removeToken
} from "@/utils/auth";
import defAva from "@/assets/logo.png";
import {
  Toast,
  Notify
} from 'vant';

const user = {
  state: {
    token: getToken(),
    indexofapp: window.indexOfApp,
    name: "",
    avatar: defAva
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token;
    },
    SET_INDEXOFAPP: (state, indexofapp) => {
      state.indexofapp = indexofapp;
    },
    SET_NAME: (state, name) => {
      state.name = name;
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar;
    }
  },

  actions: {
    // 注册
    CreateUser({
      commit
    }, userInfo) {
      return new Promise((resolve, reject) => {
        CreateUser(userInfo).then((res) => {
          if (res.Code == 0) {
            console.log(window.indexOfApp)
            if (window.indexOfApp == true) {
              setToken(res.Data);
              commit("SET_TOKEN", res.Data);
            }
            resolve();
          } else {
            Notify({
              type: 'danger',
              message: res.Message
            });
            reject(error);
          }
        }).catch((error) => {
          reject(error);
        });
      });
    },
    // 登录
    Login({
      commit
    }, userInfo) {
      return new Promise((resolve, reject) => {
        login(userInfo).then((res) => {
          if (res.Code == 0) {
            setToken(res.Data);
            commit("SET_TOKEN", res.Data);
            resolve(res);
          } else {
            Notify({
              type: 'danger',
              message: res.Message
            });
            reject(res.Message);
          }
        }).catch((error) => {
          reject(error);
        });
      });
    },
    // 修改用户信息
    UpdateUser({
      commit
    }, userInfo) {
      return new Promise((resolve, reject) => {
        UpdateUser(userInfo).then((res) => {
          if (res.Code == 0) {
            setToken(res.Data);
            commit("SET_TOKEN", res.Data);
          } else {
            Notify({
              type: 'danger',
              message: res.Message
            });
          }
          resolve(res);
        }).catch((error) => {
          reject(error);
        });
      });
    },
    // 获取用户信息
    GetMemberInfo({
      commit,
      state
    }, userInfo) {
      return new Promise((resolve, reject) => {
        GetMemberInfo(userInfo).then((res) => {
            if (res.Code == 0) {
              setToken(res.Data);
              commit("SET_TOKEN", res.Data);
            } else {
              Notify({
                type: 'danger',
                message: res.Message
              });
            }
            resolve(res);
          })
          .catch((error) => {
            reject(error);
          });
      });
    },

    // 前端 登出
    FedLogOut({
      commit
    }) {
      return new Promise((resolve) => {
        commit("SET_TOKEN", "");
        removeToken();
        resolve();
      });
    }
  }
};

export default user;